package com.himalayan.bear.controller;

import com.alibaba.fastjson.JSONObject;
import com.himalayan.bear.common.util.StringUtils;
import com.himalayan.bear.entity.Folder;
import com.himalayan.bear.entity.User;
import com.himalayan.bear.service.FolderService;
import com.himalayan.bear.util.MD5Util;
import com.himalayan.bear.util.PinyinUtil;
import com.himalayan.bear.util.RespUtil;
import com.himalayan.bear.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjk
 * @since 2019-05-30
 */
@Controller
@RequestMapping("/folder")
public class FolderController extends BaseController {

    @Autowired
    protected FolderService folderService;

    /**
     * 新建文件夹操作
     *
     * @param folder
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/new")
    public JSONObject newFolder(Folder folder, HttpSession session) {
        User user = getUser(session);
        if (user == null) {
            return RespUtil.FAIL;
        }
        if (StringUtils.isEmpty(folder.getParentId())) {
            folder.setParentId("0");
        }
        String folderName = folder.getFolderName();
        if (StringUtils.isEmpty(folderName)) {
            return RespUtil.msg(false, "文件夹名必须填写");
        }
        String pinyin = PinyinUtil.getPingYin(folderName);
        String code = MD5Util.MD5(pinyin);
        boolean r = folderService.isExistsByCode(code, folder.getParentId());
        boolean isExists = false;
        String newName = null;
        if (r) {
            newName = folderService.loadNewName(folder.getFolderName());
            pinyin = PinyinUtil.getPingYin(newName);
            code = MD5Util.MD5(pinyin);
            isExists = true;
        }
        while (isExists) {
            isExists = folderService.isExistsByCode(code, folder.getParentId());
            if (isExists) {
                newName = folderService.loadNewName(newName);
                pinyin = PinyinUtil.getPingYin(newName);
                code = MD5Util.MD5(pinyin);
            }
        }
        String uuid = UUIDUtil.getUUID();
        folder.setId(uuid);
        Folder parentFolder = folderService.getById(folder.getParentId());
        if (parentFolder == null) {
            folder.setFolderLevel(1);
        } else {
            folder.setFolderLevel(parentFolder.getFolderLevel() + 1);
        }
        folder.setUserId(user.getId());
        if (newName != null) {
            folder.setFolderName(newName);
        }
        folder.setCode(code);
        folder.setCreateDate(LocalDateTime.now());
        folder.setDeleteFlag(0);
        boolean result = folderService.save(folder);
        return RespUtil.msg(result);
    }

    /**
     * 用于面包屑导航的数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/parentList")
    public JSONObject parentList(String currentDirectoryId) {
        List<Map<String, Object>> listParent = folderService.parentList(currentDirectoryId);
        return RespUtil.msg(true, "", listParent);
    }
}
